﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>档号格式</title>
	<jsp:include page="/zheng-admin-plugins/plugins/inc/head.jsp" flush="true"/>
</head>
<style>
	.one{
		float: left;
	}
	#main{
		width: 30%;
		height: 100%;
	}
	#mainright{
		width: 70%;
		height: 100%;
		border-left:  2px;
		box-shadow: 0px 2px 10px #E8E8E8;
		padding-left: 50px;
	}
	#fonds {
		position: relative;
		width: 90%;
	}
	.node_name{
		/*font-size: 14px;*/
	}
	#fondsSpan{
		/*font-size: 16px;*/
		font-weight: bold;
	}
	/*#files{
		font-size: 16px;
		font-weight: bold;
		margin: 20px;
		margin-top: 20px;
	}*/
	/*#tableDiv {
		!*width:80%;*!
		margin-left: 100px;
		margin-top: 50px;
	}*/
	/*#fileNumTable{
		width:80%;
		margin-top: 20px;
	}*/
	th {
		/*font-size: 14px;*/
		height: 50px;
		padding-left: 10px;
	}
	a {
		margin: 2px;
		/*font-size: 13px;*/
	}
	.delA{
		color: red;
	}
</style>
<body style="font-size:14px;">
<div id="main" class="one">
	<span id="fondsSpan">请选择全宗</span>
		<select  id="fonds" onchange="changeFonds()">
		</select>
	<div style="width:auto;height:auto;overflow: auto">
		<ul class="ztree" id="tree"></ul>
	</div>
</div>
<div id="mainright" class="one">
	<div>
		<br><br>
		<shiro:hasPermission name="upms:filenum:create">
			<a class="waves-effect waves-button" href="javascript:;" onclick="insertRow()" ><i class="zmdi zmdi-plus"></i> 添加组成项</a>
		</shiro:hasPermission>
		&nbsp;&nbsp;&nbsp;
		<shiro:hasPermission name="upms:filenum:read">
			<a class="waves-effect waves-button" href="javascript:;" id="reform" onclick="reformFileNum()" ><i class="zmdi zmdi-replay"></i> 还原设置</a>
		</shiro:hasPermission>
	</div>
	<div id="tableDiv" style="margin: 20px">
		<div><b>档号构成项:</b></div>
		<table id="fileNumTable" class="table-bordered table-hover" style="margin-top: 10px;">
			<thead>
			<tr>
				<th style='display: none'></th>
				<th>著录项</th>
				<th style="width: 100px">长度</th>
				<th>分隔符</th>
				<th>操作</th>
				<th style='display: none'>id</th>
			</tr>
			</thead>
			<tbody></tbody>
		</table>
		<div style="margin-top: 50px;">
			<b>档号格式：</b><br><br>
			<input id="fullName" readonly style="width: 600px;border:none;background: none;color: red" >
		</div>
	</div>

</div>
<jsp:include page="/zheng-admin-plugins/plugins/inc/footer.jsp" flush="true"/>
<script>
	var separator = '';
	var codes = '';
	$("#tableDiv").hide();
	$(function () {
        var fonds = $("#fonds");
        $("#reform").hide();
        $.ajax({
            type : "post",
            url : "${basePath}/manage/fonds/list",
            dataType : "json",
            success : function (data) {
                $.each(data,function(i,items){
                    fonds.append("<option value='"+items.fondsId+"'>"+items.fondsNum+"&nbsp;&nbsp;"+items.fondsName+"</option>");
                });
                $.ajax({
                    type : "post",
                    url : "${basePath}/manage/filenum/read",
                    dataType : "json",
                    async:false,
                    success : function (data) {
                        selectType(data);
                        $.ajax({
                            url: "${basePath}/manage/filenum/selectSeparator",
                            type:"get",
                            dataType:"json",
							success:function (result) {
                                var str = '';
                                $.each(result,function (i,item) {
                                    str += "<option value='"+item.code+"'>"+item.description+"</option>"
                                });
                                separator = str;
                            }
						});
                    }
                })
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: "请求错误",
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
            }
        },JSON);
	});
    window.onresize = function () {
        var fondsWidth = $("#main").width();
        $(".select2").css("width",fondsWidth+"px");
        $(".ztree").css("width","auto");
    }
    var zNodes = [];
    function selectType(obj) {
        var setting = {
            data: {
                simpleData: {
                    enable: true
                }
            },
            // 回调函数
            callback : {
                onClick: clickZtree
            }
        };
        zNodes = obj;
        $.fn.zTree.init($("#tree"), setting,zNodes);
    }
    //tree点击事件
    function clickZtree(event, treeId, treeNode, clickFlag){
        var fonds = $("#fonds");
        fondsId = fonds.val();
        var pkCode = treeNode.id;
            selectCode(pkCode);
            $("#fileNumTable tbody").empty('');
            selectFileNum(fondsId,pkCode);
            $("#reform").show();
    }


    //请求对应的著录项
    function selectCode(pkCode) {
        $.ajax({
            type : "get",
            url:"${basePath}/manage/filenum/selectCode",
            dataType:"json",
            data:{
                "pkCode":pkCode
            },
            async:false,
            success:function (result) {
                var str = '';
                $.each(result,function (i,item) {
                    str += "<option value='"+item.code+"'>"+item.description+"</option>"
                });
                codes = str;
            }
        });
    }

    //请求对应的档号项
    function selectFileNum(fondsId,pkCode) {
		$.ajax({
            type : "post",
		    url:"${basePath}/manage/filenum/index",
			dataType:"json",
			data:{
		        "fondsId":fondsId,
				"pkCode":pkCode
            },
            success:function (result) {
		        if (result!=null && result.length>0){
                    $("#fileNumTable tbody").empty('');
		            $("#tableDiv").show();
                    $.each(result,function (i,item) {
						$("#fileNumTable").append(
						    "<tr>"+
								"<td style='display: none'>"+item.sortNum+"</td>"+
                            	"<td><select style='width:300px;padding:5px;margin:5px;' onchange='changeCode(this)' ></select></td>"+
                            	"<td></td>"+
                            	"<td><select style='width:100px;padding:5px;margin:5px;' onchange='changeSymbol(this)'></select></td>"+
								"<td style='padding:0 20px;'>" +
									"<shiro:hasPermission name='upms:filenum:update'><a onclick='updateFileNum(this)'> 保存</a></shiro:hasPermission>" +
									"<shiro:hasPermission name='upms:filenum:delete'><a class='delA' onclick='delFileNum(this)'> 删除</a></shiro:hasPermission>" +
									"<shiro:hasPermission name='upms:filenum:update'><a href='#' onclick='moveTr(this,1)' class='up'> 上移</a> <a href='#' onclick='moveTr(this,2)' class='down'> 下移</a></shiro:hasPermission>"+
								"</td>"+
                            	"</td><td style='display: none' >"+item.fileId+"</td>"
							+"</tr>"
						);
						//当前table的最后一行
						var row = $("#fileNumTable tr:last");
						//著录项下拉框和分隔符下拉框
						var sel = row.find("select:eq(0)");
                        sel.append(codes);
                        //sel.val(item.code).select2();
                        sel.val(item.code).css("selected");
                        var sel2 =row.find("select:last");
                        sel2.append(separator);
                        //sel2.val(item.separator).select2();
                        sel2.val(item.separator).css("selected");
                        //长度
						var len = $("#fileNumTable tr:last td:eq(2)");
						if (item.length ==null || item.length ==0){
                            len.append("<input style='padding:5px;margin:5px;width:90px;display:none' >")/*style='padding:5px;width:100%;display:none' readonly='true'*/
						}else{
						    len.append("<input style='padding:5px;margin:5px;width:90px;' value='"+item.length+"'>")
						}
                    });
					//获取档号表中的数据，拼成档号
                    var trList = $("#fileNumTable tbody").find("tr");
                    var fullName = "";
                    for(var i = 0;i<trList.length;i++){
                         var tdList = trList.eq(i).find("td");
                         var sel = tdList.find("select:eq(0)").find("option:selected").text();
                         var sel2 =tdList.find("select:eq(1)").find("option:selected").text();
                        fullName +=  sel+sel2;
                    }
                    var lastFullName = fullName.substring(0,fullName.length-1);
					$("#fullName").val(lastFullName);
                }else{
                    $("#tableDiv").hide();
                }
            }
		});
    }

    //改变著录项下拉框
	function changeCode(obj) {
        var nowTr = $(obj).parents('tr');
        var nowCode =  nowTr.find("select:eq(0)").val();
        $.ajax({
            type:"post",
			url:"${basePath}/manage/filenum/code",
			dataType:"json",
			data:{
			    "nowCode":nowCode
            },
            success:function (result) {
                var nowTd = nowTr.children("td:eq(2)");
                var nowInput = nowTd.find("input");
				if (result == 999){
                    nowInput.val('');
                    //nowInput.attr("readOnly",true);
                    //nowInput.css("background-color","#F0F0F0");
					nowInput.css("display","none");
                }else{
                    nowInput.val(result);
					//nowInput.removeAttr("readOnly");
					//nowInput.css("background-color","#FFFFFF");
					nowInput.css("display","inline");
                }
            }
		});
    }
    //给表添加一行
	function insertRow() {
        var fondsId = $("#fonds").val();
        if (fondsId !=null){
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            //获取选中的节点
            var nodes = treeObj.getSelectedNodes();
            if (nodes.length>0){
                var pkCode = nodes[0].id;
                if (pkCode != 0){
                    $("#tableDiv").show();
                    var lastNum = $("#fileNumTable tr:last td:first").text();
                    selectCode(pkCode);
                    $("#fileNumTable tbody").append(
                        "<tr>"+
                        "<td style='display: none'></td>"+
                        "<td><select style='width:300px;padding:5px;margin:5px;' onchange='changeCode(this)' ></select></td>"+
                        "<td><input style='padding:5px;margin:5px;width:90px;display:none'></td>"+
                        "<td><select style='width:100px;padding:5px;margin:5px;' changeSymbol(this)></select></td>"+
                        "<td style='padding:0 20px;'>" +
						"<shiro:hasPermission name='upms:filenum:create'><a onclick='saveFileNum(this)'> 保存</a>"+
						"<a class='delA' onclick='delFileNum(this)'> 删除</a></shiro:hasPermission></td>"+
                        "</td><td style='display: none' ></td>"+
                        +"</tr>"
                    );
                    //当前table的最后一行
                    var row = $("#fileNumTable tr:last");
                    //序号
                    var newLastNum = eval(lastNum+"+"+'1');
                    row.find("td:first").text(newLastNum);
                    //著录项下拉框和分隔符下拉框
                    var sel = row.find("select:eq(0)");
                    sel.append(codes);
                    var select2 =row.find("select:last");
                    select2.append(separator);
                }else{
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: "此目录不能添加档号格式",
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
			}else{
                $.confirm({
                    theme: 'dark',
                    animation: 'rotateX',
                    closeAnimation: 'rotateX',
                    title: false,
                    content: "请选择档案分类",
                    buttons: {
                        confirm: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button waves-light'
                        }
                    }
                });
			}

		}else{
            $.confirm({
                theme: 'dark',
                animation: 'rotateX',
                closeAnimation: 'rotateX',
                title: false,
                content: "请选择全宗",
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button waves-light'
                    }
                }
            });
		}


    }
    //重整档号
    function reformFileNum(){
        var fondsId = $("#fonds").val();
        if (fondsId !=null && fondsId.length>0){
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            //获取选中的节点
            var nodes = treeObj.getSelectedNodes();
            var pkCode = nodes[0].id;
			selectFileNum(fondsId,pkCode);

		}else{
            $.confirm({
                theme: 'dark',
                animation: 'rotateX',
                closeAnimation: 'rotateX',
                title: false,
                content: "请选择全宗",
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button waves-light'
                    }
                }
            });
		}
	}

	//添加档号格式
	function saveFileNum(obj) {
        var oneTr =  $(obj).parents('tr');
        //所属id
        var id= oneTr.children("td:last").text();
        //序号
		var sortNum = oneTr.children("td:first").text();
		//著录项code
        var code = oneTr.find("select:eq(0)").val();
        //长度
        var len = oneTr.find("input").val();
        //分隔符
		var separator = oneTr.find("select:eq(1)").val();
		//pkCode
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        var pkCode = nodes[0].id;
        //全宗id
		var str1 = $("#fonds").val();
        $.ajax({
		    type:"post",
		    url:"${basePath}/manage/filenum/create",
			dataType:"json",
			data:{
		        "fileId":id,
				"sortNum":sortNum,
				"code":code,
				"length":len,
				"separator":separator,
				"pkCode":pkCode,
				"str1":str1
            },
            success:function (result) {
				if (result == 1 ){
                    $("#fileNumTable tbody").empty('');
					selectFileNum(str1,pkCode);
                }else if(result == 4){
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: "该档号项已存在",
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
            }
		});
    }
	//编辑档号格式
	function updateFileNum(obj) {
        var oneTr =  $(obj).parents('tr');
        //所属id
        var id= oneTr.children("td:last").text();
        //序号
        var sortNum = oneTr.children("td:first").text();
        //著录项code
        var code = oneTr.find("select:eq(0)").val();
        //长度
        var len = oneTr.find("input").val();
        //分隔符
        var separator = oneTr.find("select:eq(1)").val();
        //pkCode
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        var pkCode = nodes[0].id;
        //全宗id
        var str1 = $("#fonds").val();
        $.ajax({
            type:"post",
            url:"${basePath}/manage/filenum/update1",
            dataType:"json",
            data:{
                "fileId":id,
                "sortNum":sortNum,
                "code":code,
                "length":len,
                "separator":separator,
                "pkCode":pkCode,
                "str1":str1
            },
            success:function (result) {
                if (result == 1 ){
                    $("#fileNumTable tbody").empty('');
                    selectFileNum(str1,pkCode);
                }else if(result == 2){
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: "此档号项不可编辑名称",
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }else if(result == 3){
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: "编辑成功",
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }else if(result == 4){
                    $.confirm({
                        theme: 'dark',
                        animation: 'rotateX',
                        closeAnimation: 'rotateX',
                        title: false,
                        content: "该档号项已存在",
                        buttons: {
                            confirm: {
                                text: '确认',
                                btnClass: 'waves-effect waves-button waves-light'
                            }
                        }
                    });
                }
            }
        });
    }
    //删除档号格式
	function delFileNum(obj) {
        $.confirm({
            title: false,
            content: '确定删除此档号项？',
            backgroundDismiss: true,
            buttons: {
                ok: {
                    text: '确定',
                    btnClass: 'waves-effect waves-button',
                    action: function (){
                        var oneTr =  $(obj).parents('tr');
                        //所属id
                        var id= oneTr.children("td:last").text();
                        if (id !=null && id>0){
                            $.ajax({
                                type:"post",
                                url:"${basePath}/manage/filenum/delete",
                                dataType:"json",
                                data:{
                                    "fileId":id
                                },
                                success:function (result) {
                                    if (result==1){
                                        oneTr.remove();
                                        //获取档号表中的数据，拼成档号
                                        var trList = $("#fileNumTable tbody").find("tr");
                                        var fullName = "";
                                        for(var i = 0;i<trList.length;i++){
                                            var tdList = trList.eq(i).find("td");
                                            var sel = tdList.find("select:eq(0)").find("option:selected").text();

                                            var sel2 =tdList.find("select:eq(1)").find("option:selected").text();
                                            fullName +=  sel+sel2;
                                        }
                                        var lastFullName = fullName.substring(0,fullName.length-1);
                                        $("#fullName").val(lastFullName);
                                    }else if (result==2){
                                        $.confirm({
                                            theme: 'dark',
                                            animation: 'rotateX',
                                            closeAnimation: 'rotateX',
                                            title: false,
                                            content: "此档号组成项不可删除",
                                            buttons: {
                                                confirm: {
                                                    text: '确认',
                                                    btnClass: 'waves-effect waves-button waves-light'
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        }else{
                            oneTr.remove();
                        }
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
    }

    //上下移
	function moveTr(obj,num) {
        //pkCode
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        //获取选中的节点
        var nodes = treeObj.getSelectedNodes();
        var pkCode = nodes[0].id;
        //全宗id
        var str1 = $("#fonds").val();
        var $tr = $(obj).parents("tr");
        if (num == 1){
            //上移
            if ($tr.index() != 0) {
                //上一个的序号 和 id
                var num1 = $tr.prev().find("td:first").text();
                var id1 = $tr.prev().find("td:last").text();
                //当前的序号 和 id
				var num2 = $tr.find("td:first").text();
				var id2 = $tr.find("td:last").text();
                //去后台修改交换对象的序号
                $.ajax({
                    type:"post",
                    url:"${basePath}/manage/filenum/update",
                    dataType:"json",
                    data:{
                        "id1":id1,
						"num2":num2,
						"id2":id2,
						"num1":num1
                    },
                    success:function (result) {
                        if (result == 2){
                            $tr.prev().find("td:first").text(num2);
                            $tr.find("td:first").text(num1);
                            $tr.fadeOut().fadeIn();
                            $tr.prev().before($tr);
                            //selectFileNum(str1,pkCode);
                            //获取档号表中的数据，拼成档号
                            var trList = $("#fileNumTable tbody").find("tr");
                            var fullName = "";
                            for(var i = 0;i<trList.length;i++){
                                var tdList = trList.eq(i).find("td");
                                var sel = tdList.find("select:eq(0)").find("option:selected").text();

                                var sel2 =tdList.find("select:eq(1)").find("option:selected").text();
                                fullName +=  sel+sel2;
                            }
                            var lastFullName = fullName.substring(0,fullName.length-1);
                            $("#fullName").val(lastFullName);
                        }
                    }
                });
            }
		}else{
            //下移
            var $down = $(".down");
            var len = $down.length;
			if ($tr.index() != len-1) {
                //下一个的序号
                var num1 = $tr.next().find("td:first").text();
                var id1 = $tr.next().find("td:last").text();
                //当前的序号
                var num2 = $tr.find("td:first").text();
                var id2 = $tr.find("td:last").text();
                //去后台修改交换对象的序号
                $.ajax({
                    type:"post",
                    url:"${basePath}/manage/filenum/update",
                    dataType:"json",
                    data:{
                        "id1":id1,
                        "num1":num1,
                        "id2":id2,
                        "num2":num2
                    },
                    success:function (result) {
                        if (result==2){
                            //更换序号
                            $tr.find("td:first").text(num1);
                            $tr.next().find("td:first").text(num2);
                            $tr.fadeOut().fadeIn();
                            $tr.next().after($tr);
                            //selectFileNum(str1,pkCode);
                            //获取档号表中的数据，拼成档号
                            var trList = $("#fileNumTable tbody").find("tr");
                            var fullName = "";
                            for(var i = 0;i<trList.length;i++){
                                var tdList = trList.eq(i).find("td");
                                var sel = tdList.find("select:eq(0)").find("option:selected").text();

                                var sel2 =tdList.find("select:eq(1)").find("option:selected").text();
                                fullName +=  sel+sel2;
                            }
                            var lastFullName = fullName.substring(0,fullName.length-1);
                            $("#fullName").val(lastFullName);
                        }
                    }
                });
			}
		}
    }
    
    //当改变全宗时
	function changeFonds() {
        $("#tableDiv").hide();
        $("#reform").hide();
    }

    function changeSymbol(obj) {
        var trList = $("#fileNumTable tbody").find("tr");
        var fullName = "";
        for(var i = 0;i<trList.length;i++){
            var tdList = trList.eq(i).find("td");
            var sel = tdList.find("select:eq(0)").find("option:selected").text();

            var sel2 =tdList.find("select:eq(1)").find("option:selected").text();
            fullName +=  sel+sel2;
        }
        var lastFullName = fullName.substring(0,fullName.length-1);
        $("#fullName").val(lastFullName);
    }
</script>
</body>
</html>